package javagprssrv.Listener;

import javagprssrv.Common.Commands;
import javagprssrv.StructClass.structRecvData;
import javagprssrv.Log;
import javagprssrv.Common.CommonClass;
import java.net.DatagramPacket;
import javagprssrv.JGS_Param;
import java.net.DatagramSocket;

public class TrdYiBiaoDataListener_UDP extends java.lang.Thread
{
    static final int MAX_PACKET_SIZE = 65507;
    private int UDPServerSocketIndex;
    private java.net.DatagramSocket socket;
    public java.lang.String RemoteIP;
    public int RemotePort;
    
    public TrdYiBiaoDataListener_UDP(final java.net.DatagramSocket _socket) {
        super("TrdYiBiaoDataListener_UDP");
        this.socket = _socket;
    }
    
    public TrdYiBiaoDataListener_UDP(final int intUDPServerSocketIndex) {
        super("TrdYiBiaoDataListener_UDP");
        this.UDPServerSocketIndex = intUDPServerSocketIndex;
        this.socket = ((DatagramSocket)javagprssrv.JGS_Param.UDPServerSockets.elementAt(this.UDPServerSocketIndex));
    }
    
    @java.lang.Override
    public void run() {
        final byte[] buffer = new byte[65507];
        final java.net.DatagramPacket data = new java.net.DatagramPacket(buffer, buffer.length);
        while (true) {
            if (!javagprssrv.JGS_Param.isSystemReady) {
                try {
                    java.lang.Thread.sleep(10L);
                }
                catch (java.lang.Exception e) {}
            }
            else {
                try {
                    this.socket.receive(data);
                    this.RemoteIP = data.getAddress().getHostAddress();
                    this.RemotePort = data.getPort();
                    byte[] r = new byte[data.getLength()];
                    java.lang.System.arraycopy(data.getData(), 0, r, 0, r.length);
                    final java.lang.String recv = javagprssrv.Common.CommonClass.ByteArray2String(r, r.length);
                    java.lang.String s = "[P=UDP,T=GET " + java.lang.Integer.toString(data.getLength()) + ",S=";
                    s = s + data.getAddress().getHostAddress() + ":";
                    s = s + java.lang.Integer.valueOf(data.getPort()).toString().trim() + "] ";
                    javagprssrv.JGS_Param.myLog.ShowLog(s + new javagprssrv.Log().String2HexString(recv, " "), true, true);
                    javagprssrv.StructClass.structRecvData recvData = new javagprssrv.StructClass.structRecvData();
                    recvData.Protocol = "UDP";
                    recvData.RemoteIP = this.RemoteIP;
                    recvData.RemotePort = this.RemotePort;
                    recvData.UDPServerSocketIndex = this.UDPServerSocketIndex;
                    recvData.Data = recv;
                    new javagprssrv.Common.Commands().Do_ExtendCommands(recv);
                    javagprssrv.JGS_Param.UnDoneRecvDatas.add(recvData);
                    recvData = null;
                    r = null;
                    this.setUDPDataThroughput(data.getLength());
                }
                catch (java.lang.Exception e) {
                    javagprssrv.JGS_Param.myLog.ShowLog("[ TrdYiBiaoDataListener_UDP.Run() ERROR ] " + e.getMessage(), true, true);
                }
            }
        }
    }
    
    private synchronized void setUDPDataThroughput(final int DataSize) {
        long a = 0L;
        long b = 0L;
        long c = 0L;
        a = javagprssrv.JGS_Param.countReceiveData_UDP + DataSize;
        b = javagprssrv.JGS_Param.timesReceiveData_UDP + 1L;
        c = javagprssrv.JGS_Param.countReceiveData_All + DataSize;
        javagprssrv.JGS_Param.countReceiveData_UDP = a;
        javagprssrv.JGS_Param.timesReceiveData_UDP = b;
        javagprssrv.JGS_Param.countReceiveData_All = c;
    }
}
